package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.CustomException;
import com.dto.DishDto;
import com.entity.Dish;
import com.entity.DishFlavor;
import com.mapper.DishMapper;
import com.service.DishFlavorService;
import com.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Override
    @Transactional//添加事务管理
    //增添数据（菜品信息和口味）
    public void saveWithFlavor(DishDto dishDto) {
        this.save(dishDto);//将菜品信息保存到菜品当中
        Long id = dishDto.getId();//获取当前菜品的id,关联口味表
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor f:flavors){
                f.setDishId(id);
        }
        dishFlavorService.saveBatch(flavors);
    }
    //修改前回显数据
    @Override
    public DishDto getWithFlavor(Long id) {
        DishDto dishDto=new DishDto();//创建dishDto类，包含其菜品和口味的属性
        Dish dish = this.getById(id);
        BeanUtils.copyProperties(dish,dishDto);
        LambdaQueryWrapper<DishFlavor> lqw=new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,id);
        List<DishFlavor> dishFlavors = dishFlavorService.list(lqw);
        dishDto.setFlavors(dishFlavors);
        return dishDto;
    }

    //修改数据
    @Override
    @Transactional//添加事务管理
    public void updateWithFlavor(DishDto dishDto) {
        //修改菜品数据
        this.updateById(dishDto);
        //删除口味
        Long id = dishDto.getId();//获取该类的dish_id
        LambdaQueryWrapper<DishFlavor> lqw=new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,id);
        dishFlavorService.remove(lqw);
        //增加修改的口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor f:flavors){
            f.setDishId(id);
        }
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    @Transactional//添加事务管理
    public void delete(List<Long> ids) {
        LambdaQueryWrapper<Dish> lqw1=new LambdaQueryWrapper<>();
        lqw1.in(Dish::getId,ids);//删除id集合所包含的菜品
        lqw1.eq(Dish::getStatus,1);//观察是否起售，若起售则不能删除
        long count = this.count(lqw1);
        if (count>0){
            throw new CustomException("菜品为起售状态，删个der");
        }
        this.removeByIds(ids);//删除菜品信息
        LambdaQueryWrapper<DishFlavor> lqw2=new LambdaQueryWrapper<>();
        lqw2.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(lqw2);
    }

    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        //批量起售
        LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<>();
        lqw.in(Dish::getId,ids);
        List<Dish> dishList = this.list(lqw);
        List<Dish> newList=new ArrayList<>();
        if (status==1){
            //批量起售
            for (Dish d:dishList){
                Dish d2=new Dish();
                BeanUtils.copyProperties(d,d2);
                d2.setStatus(1);
                newList.add(d2);
            }
        }
        if (status==0){
            //批量停售
            for (Dish d:dishList){
                Dish d2=new Dish();
                BeanUtils.copyProperties(d,d2);
                d2.setStatus(0);
                newList.add(d2);
            }
        }
        this.updateBatchById(newList);
    }

}
